package com.apexedu.eas.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.apexedu.eas.common.SysProperty;
import com.apexedu.eas.vo.request.FeeitemReq;

import cn.com.sr.commonj.encrypt.jdk.MD5Util;

public class HttpClientUtil {

	/**
	 * 
	 * @param url
	 * @param args get请求方式不需要传该参数。
	 * @param isPost
	 * @return
	 */
	public static String request(String url, Map<String, String> args, boolean isPost) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		try {
			if (isPost) {
				HttpPost httppost = new HttpPost(url);
				List<NameValuePair> formparams = new ArrayList<NameValuePair>();
				if (args != null) {
					Set<Entry<String, String>> set = args.entrySet();
					for (Entry<String, String> e : set) {
						formparams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
					}
				}
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
				httppost.setEntity(entity);
				response = httpclient.execute(httppost);
			} else {
				HttpGet httpget = new HttpGet(url);
				response = httpclient.execute(httpget);
			}
			int code = response.getStatusLine().getStatusCode();
			//System.out.println(code);
			//if(code==302)
				//System.out.println("http://211.64.79.51:8081"+response.getLastHeader("Location").getValue());
			if (code == 200)
				return EntityUtils.toString(response.getEntity());
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static void main(String[] args) {
		/*Map<String, String> arg =new HashMap<String, String>();
		arg.put("userId", "admin");
		arg.put("pass", "27c36f6ffa3e72759f7d464b10f72410");
		System.out.println(request("http://211.64.79.147:8080/login.do", arg, true));*/
	/*	Map<String, String> arg =new HashMap<String, String>();
		StudInfoReq req = new StudInfoReq();
		req.setCustno("16511006120170");
		req.setCusttype("cust_no1");//exam_no
		//req.setName("");
		String json = JasksonUtil.getJSONString(req);
		arg.put("json", json);
		String signature=MD5Util.encrypt(json+SysProperty.securetKey);
		arg.put("signature", signature);
		System.out.println("json :"+json);
		System.out.println("signature :"+signature);
		System.out.println(request("http://211.64.79.147:7030/pay/getStudInfo", arg, true));*/
	
		Map<String, String> arg =new HashMap<String, String>();
		FeeitemReq req = new FeeitemReq();
		req.setCustno("16372301150021"); //16511006120170 16510601167187 16321084680965 16320501671038
		//req.setCusttype("exam_no");
		String json = JasksonUtil.getJSONString(req);
		arg.put("json", json);
		String signature=MD5Util.encrypt(json+SysProperty.securetKey);
		arg.put("signature", signature);
		System.out.println("json :"+json);
		System.out.println("signature :"+signature);
		System.out.println(request("http://211.64.79.147:7030/pay/getFeeitem", arg, true));
		/*Map<String, String> arg =new HashMap<String, String>();
		NotifyResultReq req = new NotifyResultReq();
		req.setCustno("15370881150783");
		req.setAmt("50");
		req.setClienttype("web");
		req.setSerialnum("000001");
		req.setStatus("00");
		req.setSettledate("20170526");
		List<com.apexedu.eas.vo.request.Sub> list =new ArrayList<Sub>();
		Sub sub = new Sub();
		sub.setFeeid("1355");
		sub.setAmt("50");
		list.add(sub);
		req.setSubs(list);
		//req.setCusttype("exam_no");
		String json = JasksonUtil.getJSONString(req);
		arg.put("json", json);
		String signature=MD5Util.encrypt(json+SysProperty.securetKey);
		arg.put("signature", signature);
		System.out.println("json :"+json);
		System.out.println("signature :"+signature);
		System.out.println(request("http://localhost/pm/pay/notifyResult", arg, true));*/
		
	/*	Map<String, String> arg =new HashMap<String, String>();
		ToPayReq req = new ToPayReq();
		req.setCustno("15370881150783"); //16511006120170 16510601167187 16321084680965 16320501671038
		req.setAccounttype("cust_no");
		//req.setCusttype("cust_no");
		req.setFront_skip_url("http://211.64.79.147:7050/s/index.html");
		//req.setFront_notify_url("http://211.64.79.147:7050/s/index.html");
		String json = JasksonUtil.getJSONString(req);
		//json=json.replaceAll("\r\n", "");
		
		arg.put("json", json);
		String signature=MD5Util.encrypt(json+SysProperty.securetKey);
		System.out.println(SysProperty.securetKey);
		arg.put("signature", StringUtil.lowerCase(signature));
		System.out.println("json :"+json);
		System.out.println("signature :"+StringUtil.lowerCase(signature));
		System.out.println(request("http://211.64.79.51:8081/school/interface/toPay", arg, true));*/

	}

}
